﻿using Common.Core.Helpers.Json;
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;

namespace Common.Core.Base
{
    /// <summary>
    /// Web Api基类，封装一下公共函数，此类公共到其他项目用的
    /// </summary>
    public class BAC : Controller
    {
        /// <summary>
        /// 重写基类在Action执行之前的事情
        /// 根据token获得当前用户，允许匿名的不需要获取用户
        /// </summary>
        /// <param name="filterContext">重写方法的参数</param>
        //public override void OnActionExecuting(ActionExecutingContext filterContext)
        //{
        //
        //}

        /// <summary>
        /// 返回成功信息
        /// </summary>
        /// <param name="data">附加内容</param>
        /// <param name="msg">成功信息</param>
        /// <param name="code">编码</param>
        /// <returns>返回Result格式信息</returns>
        [NonAction]
        public IActionResult SuccessResult(object data = null, string msg = "成功", string code = "200", bool isNull = false)
        {
            return ToJsonContent(new Result<object>()
            {
                Code = code,
                Msg = msg,
                Data = data == null ? new { } : data
            }, isNull);
        }

        /// <summary>
        /// 返回错误信息
        /// </summary>
        /// <param name="msg">错误信息</param>
        /// <param name="code"></param>
        /// <returns>返回Result格式信息</returns>
        [NonAction]
        public IActionResult ErrorResult(string msg, string code = "0", bool isNull = false)
        {
            return ToJsonContent(new Result<object>()
            {
                Code = code,
                Msg = msg
            },isNull);
        }

        public IActionResult ToJsonContent(object obj, bool isNull = false)
        {
            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = true,                                         //格式化json字符串
                AllowTrailingCommas = true,                                   //可以结尾有逗号
                DefaultIgnoreCondition = isNull ? JsonIgnoreCondition.WhenWritingNull :
                    JsonIgnoreCondition.Never,                                //可以有空值,转换json去除空值属性
                IgnoreReadOnlyProperties = true,                              //忽略只读属性
                PropertyNameCaseInsensitive = true,                           //忽略大小写
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
                //DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };
            options.Converters.Add(new DateTimeJsonConverter());
            return Content(JsonSerializer.Serialize(obj, options));
        }

        public IActionResult ToFileStream(FileResult<Stream> obj, bool isNull = false)
        {
            if (obj != null&&obj.Code=="200")
            {
                return File(obj.Data, obj.FileContentType,obj.FileName+".xls");
            }
            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = true,                                         //格式化json字符串
                AllowTrailingCommas = true,                                   //可以结尾有逗号
                DefaultIgnoreCondition = isNull ? JsonIgnoreCondition.WhenWritingNull :
                    JsonIgnoreCondition.Never,                                //可以有空值,转换json去除空值属性
                IgnoreReadOnlyProperties = true,                              //忽略只读属性
                PropertyNameCaseInsensitive = true,                           //忽略大小写
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
                //DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };
            return Content(JsonSerializer.Serialize(obj, options));
        }
    }
}
